<?php

namespace app\store\service\game;

use app\common\service\BaseService;
use app\store\model\game\Room as RoomModel;
use app\common\service\game\Live;
use think\facade\Cache;

class Room extends BaseService
{
    const Roomconfig = 'Roomconfig:';
    public function getIndex($params):array
    {
        $condition = [];
        if(!empty($params['room_id'])){
            $condition[] = ['room_id','=',$params['room_id']];
        }
        $condition[] = ['is_del','=',0];
        return (new RoomModel())
            ->where($condition)
            ->paginate($params['limit'] ?? 15)
            ->toArray();
    }

    /**保存
     * @param $params
     * @return bool
     */
    public function save($params):bool
    {
        $streaming_url = (new Live())->creatStreamUrl($params['room_id'],$params['device_id']);
        if(!is_array($params['table_id'])){
            throwError('table_id参数格式错误');
        }
        if(empty($params['id'])){
            //排重
            $room = (new RoomModel())->where('room_id',$params['room_id'])->lock(true)->find();
            if($room){
               throwError('已存在room_id,请尝试其他room_id');
            }
            $room = (new RoomModel())::create([
                'room_id'  => $params['room_id'],
                'room_name' => $params['room_name'],
                'device_id' => $params['device_id'],
                'game_name' => $params['game_name'],
                'web_url' => $params['web_url'],
                'web_port' => $params['web_port'],
                'num' => $params['num'],
                'power' => $params['power'],
                'server_ip' => $params['server_ip'],
                'server_port' => $params['server_port'],
                'table_id' => implode(',',$params['table_id']),
                'min_carrying' => $params['min_carrying'],
                'first_exchange' => $params['first_exchange'],
                'streaming_url' => $streaming_url
            ]);
            if(!$room->id){
                throwError('insert room error');
            }
        }else{
            //排重
            $condition = [
                ['room_id','=',$params['room_id']],
                ['id','<>',$params['id']]
            ];
            $room = (new RoomModel())->where($condition)->lock(true)->find();
            if($room){
                throwError('已存在room_id,请尝试其他room_id');
            }
            $condition = [
                ['is_del','=',0],
                ['id','=',$params['id']]
            ];
            $room = (new RoomModel())->where($condition)->find();
            if(!$room){
                throwError('房间未找到或已被删除');
            }
            $room->room_id = $params['room_id'];
            $room->room_name = $params['room_name'];
            $room->device_id = $params['device_id'];
            $room->game_name = $params['game_name'];
            $room->web_url = $params['web_url'];
            $room->web_port = $params['web_port'];
            $room->num = $params['num'];
            $room->power = $params['power'];
            $room->server_ip = $params['server_ip'];
            $room->server_port = $params['server_port'];
            $room->table_id = implode(',',$params['table_id']);
            $room->streaming_url = $streaming_url;
            $room->min_carrying = $params['min_carrying'];
            $room->first_exchange = $params['first_exchange'];
            $room->save();
        }
        //redis写入
        $params['table_id'] = json_encode($params['table_id']);
        $params['streaming_url'] = $streaming_url;
        $redis = Cache::store('redis');
        $redis->select(10);
        $redis->hmset(self::Roomconfig.$params['room_id'],$params);

        return true;
    }



}